<template>
  <div style="width: 300px">
    <el-popover v-model="cronPopover">
      <cron v-model="iconValue" @change="changeCron" @close="cronPopover=false" i18n="cn"></cron>
      <el-input size="small" slot="reference" @click="cronPopover=true" v-model="iconValue"
                placeholder="请输入定时策略"></el-input>
    </el-popover>
  </div>
</template>

<script>
import Cron from './index.vue';

export default {
  name: "ms-cron-input",
  props: {
    value: {
      type: String,
      default: '* * * * * ? *'
    }
  },
  components: {
    Cron
  },
  data() {
    return {
      cronPopover: false,
      iconValue: '* * * * * ? *'
    };
  },
  methods: {
    changeCron(val) {
      this.$emit("update:value", val)
      this.setCron(val)
    },
    setCron(newValue) {
      if (!newValue || newValue.trim().length < 11) {
        this.$message.error('格式错误');
        return;
      }
      this.iconValue = newValue
    }
  },
  watch: {
    value: {
      handler(val) {
        if (!val) {
          this.$emit("update:value", '* * * * * ? *')
        } else {
          this.setCron(val);
        }
      },
      deep: true,
      immediate: true
    }
  }
}
</script>
